#!/bin/bash

#------------------------------------------------------------------------------
#Filename:      84406-lvm.sh
#Version:       1.0
#Date:          2014/06/25
#Author:        xi.zhang
#Email:         xi.zhang@cs2c.com.cn
#Summary:       02.02系统管理功能-02逻辑卷管理-02字符命令-08创建逻辑卷快照
#Notes:         创建逻辑卷快照
#Copyright:     China Standard Software Co.,Ltd.
#History:
#               Version 1.0,2014/06/25
#               -The first one
#               Version 1.1,2014/10/22
#               -The second one
#               -Add SFROOT
#               -Rename variables
#               -Add notes
#               -Add CleanData Function
#               -Add trap
#------------------------------------------------------------------------------

if [ -z "$SFROOT" ]
then
    CMDLIB=./
    SFROOT=./
else
    CMDLIB=${SFROOT}/testcases/Management_Function
fi

. ${SFROOT}/lib/UserOps.sh
. ${CMDLIB}/lib/Partition_parted.sh
. ${CMDLIB}/lib/PV.sh
. ${CMDLIB}/lib/VG.sh
. ${CMDLIB}/lib/LV.sh

IsRoot
EchoResult "脚本执行用户为root"

export PVNUM=2                                 #物理卷数目
export VGNAME=vgroup-$$                        #卷组名称
export LVNAME=lvgroup-$$                       #逻辑卷名称
export LVSIZE=5                                #逻辑卷大小
export LVPATH=/dev/${VGNAME}/${LVNAME}         #逻辑卷路径
export LSSIZE=10                               #快照大小
export LSNAME=lvsnap-$$                        #快照名称  
export LSPATH=/dev/${VGNAME}/${LSNAME}         #快照路径  

function CleanData()
{
  EchoInfo "Umount LV Snap"
  umount /${LSNAME} && rm -fr /${LSNAME}
 
  EchoInfo "Umount LV"
  umount /${LVNAME}/ && rm -fr /${LVNAME}

  EchoInfo "Delete LV snap"
  DeleteLVsnap

  EchoInfo "Delete LV"
  DeleteLV

  EchoInfo "Delete VG"
  DeleteVG

  EchoInfo "Delete PV"
  DeletePV
}

trap CleanData INT EXIT

GetHarddisk

CreatePV
EchoResult "Step 1 :Create PV"

CreateVG
EchoResult "Step 2 :Create VG"

echo "y" | CreateLV
EchoResult "Step 3 :Create LV"

#Create filesystem
mkfs.ext4 ${LVPATH}
EchoResult "Step 4: Make filesystem"

#mount filesystem
[ -d /${LVNAME} ] && rm -fr /${LVNAME}
mkdir /${LVNAME}
mount ${LVPATH} /${LVNAME}
EchoResult "Step 5: Mount filesystem"

cp -a /root/* /${LVNAME}
EchoResult "Step 6: Write data to LV"

#EchoInfo "Sleep 5 seconds..."
#sleep 5

CreateLVsnap
EchoResult "Setp 7: Create LV snap"

[ -n `ls /dev/${VGNAME} | grep “${LSNAME}”` ]
EchoResult "Step 8: Verify  LV Snap Create"

[ -d /${LSNAME} ] && rm -fr /${LSNAME} 
mkdir /${LSNAME}
mount ${LSPATH} /${LSNAME}
df -H | tail -n 2 >lvs.txt
p1=`sed -n '2p' lvs.txt |awk '{print $1,$2,$3,$4}'`
p2=`sed -n '4p' lvs.txt |awk '{print $1,$2,$3,$4}'`
[ "$p1" = "$p2" ]
EchoResult "Step 9: Verify LV and LVS"
rm -fr lvs.txt

EchoInfo "Sleep 5 seconds..."
sleep 5
